package com.waiterxiaoyy.util;

import com.sun.mail.util.MailSSLSocketFactory;
import com.waiterxiaoyy.entity.Notice;
import com.waiterxiaoyy.entity.User;
import com.waiterxiaoyy.mapper.MailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

@Component
public class SendEmail {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private MailMapper mailMapper;
    public void sendNewUser(String userMail) {
        SimpleMailMessage mailMessage = null;
        try {
            //生成并填充消息体
            mailMessage = new SimpleMailMessage();
            mailMessage.setFrom("你的邮箱");//发送邮箱
            mailMessage.setTo(userMail);//接收邮箱
            mailMessage.setSubject("【提醒小助手】广东工业大学招生网发布了新的信息"); //邮件主题
            StringBuilder builder = new StringBuilder();
            mailMessage.setSubject("【提醒小助手】您已成功订阅消息通知"); //邮件主题
            builder.append("小哲阳阳已记录您的邮箱了，到时候广工大出新消息就会推送给您，记得查收噢!!!\n" );
            builder.append("\n招生信息官网：" + "https://yzw.gdut.edu.cn/sszs.htm");
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            builder.append("\n[注]：" + "此邮件可放心查看(来自小哲阳阳)" + "--推送时间："+ time );
            mailMessage.setText(builder.toString()); //邮件正文

            //发送邮件
            mailSender.send(mailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void sendEmail(List<Notice> newNotice) {

        try {
            //生成并填充消息体
            SimpleMailMessage mailMessage = new SimpleMailMessage();

            mailMessage.setSubject("【提醒小助手】广东工业大学招生网发布了新的信息"); //邮件主题
            StringBuilder builder = new StringBuilder();
            int count = 1;
            for(int i = newNotice.size() - 1; i >= 0; i--) {
                builder.append("第" + count + "条\n");count++;
                builder.append("标题：" + newNotice.get(i).getNoticeTitle());
                builder.append("\n通知链接：" + newNotice.get(i).getNoticeUrl());
                builder.append("\n发布时间：" + newNotice.get(i).getDate());
                builder.append("\n--------------\n");
            }

            builder.append("\n招生信息官网：" + "https://yzw.gdut.edu.cn/sszs.htm");
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            builder.append("\n[注]：" + "此邮件可放心查看(来自小哲阳阳)" + "--推送时间："+ time );
            mailMessage.setText(builder.toString()); //邮件正文

            //获取当前时间
            Date date1 = new Date();
            String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1);

            List<User> userList = mailMapper.selectUser();
            for(int i = 0; i < userList.size(); i++) {
                mailMessage.setFrom("你的邮箱");//发送邮箱
                mailMessage.setTo(userList.get(i).getUserEmail());//接收邮箱

                //发送邮件
                mailSender.send(mailMessage);
                mailMapper.updateSendTime(userList.get(i).getUserEmail(), nowTime);
                Thread.currentThread().sleep(20000);
            }

        } catch (MailException | InterruptedException e) {
            e.printStackTrace();
        }

//        Properties props = new Properties();
//
//        // 开启debug调试
//        props.setProperty("mail.debug", "true");
//        // 发送服务器需要身份验证
//        props.setProperty("mail.smtp.auth", "true");
//        // 设置邮件服务器主机名
//        props.setProperty("mail.host", "smtp.qq.com");
//        // 发送邮件协议名称
//        props.setProperty("mail.transport.protocol", "smtp");
//
//        MailSSLSocketFactory sf = null;
//        for(int i = 0; i < users.size(); i++) {
//            try {
//                sf = new MailSSLSocketFactory();
//                sf.setTrustAllHosts(true);
//                props.put("mail.smtp.ssl.enable", "true");
//                props.put("mail.smtp.ssl.socketFactory", sf);

//                Session session = Session.getInstance(props);
//                Message msg = new MimeMessage(session);
//                msg.setSubject("【提醒小助手】暨大招生网发布了新的信息");
//                StringBuilder builder = new StringBuilder();
//                builder.append("标题：" + notice.getNoticeTitle());
//                builder.append("\n通知链接：" + notice.getNoticeUrl());
//                builder.append("\n发布时间：" + notice.getDate());
//                builder.append("\n招生信息官网：" + notice.getZhaoshengUrl());
//                String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//                builder.append("\n[注]：" + "此邮件可放心查看" + "--推送时间："+ time );
//                msg.setText(builder.toString());
//                users.get(i).setSendTime(time);
//                users.get(i).setStatus(notice.getNoticeId());
//                if (memberMapper.modifyUserStatus(users.get(i)) == 1) {
//                    //邮件发送者
//                    msg.setFrom(new InternetAddress("waiterxiaoyy@qq.com"));
//                    //发送邮件
//                    Transport transport = session.getTransport();
//                    transport.connect("smtp.qq.com","waiterxiaoyy@qq.com", "zeasgojflcujbeaa");
//                    // 739609107@qq.com zbcgcaauacrpbdbf
//                    // waiterxiaoyy@qq.com zeasgojflcujbeaa
//                    transport.sendMessage(msg, new Address[]{new InternetAddress(users.get(i).getUseremail())});
//
//                    transport.close();
//                }

//                Thread.currentThread().sleep(20000);
//                System.out.println(users.get(i).getUseremail());
//            } catch (GeneralSecurityException | InterruptedException e) {
//                e.printStackTrace();
//            }
//        }

    }
}
